/**业务管理流程平台版本1.0,生成时间:2017-04-13 13:16:48**/
package com.xbdun.dao.bmp.mapper;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;

import com.xbdun.dao.bmp.entity.XbdunSysOrgDo;
import com.xbdun.dao.bmp.provider.SysOrgSqlProvider;


/**
 * 企业信息
 * @author 张国涛
 * @2017-04-13 13:16:48
 */
@Mapper
public interface SysOrgMapper{

	/**
	 * 根据orgCode获取对象
	 * @return
	 */
	@Select("SELECT * FROM xbdun_sys_org where orgCode=${orgCode}")
	public XbdunSysOrgDo getSysOrgByOrgCode(@Param("orgCode")long orgCode);
	
	/**
	* @Title: getOrgInfoByOrgCod
	* @Description: 获取机构信息
	* @param orgCode
	* @return XbdunSysOrgDo    返回类型
	 */
	@Select("SELECT * FROM xbdun_sys_org where orgCode=#{orgCode}")
	public XbdunSysOrgDo getOrgInfoByOrgCod(@Param("orgCode")Long orgCode);
	
	/**
	* @Title: uptAgentOrg
	* @Description: 设置机构是否为代理机构
	* @param orgCode		机构编号
	* @param blAgentOrg		是否为代理机构
	* @param version		版本号
	* @return    设定文件
	* @return int    返回类型
	 */
	@Update("UPDATE xbdun_sys_org SET blAgentOrg=${agentOrg} WHERE orgCode=${orgCode} AND version=#{version} AND enable = 1")
	public int uptAgentOrg(@Param("orgCode")long orgCode,@Param("agentOrg")int blAgentOrg,@Param("version")Timestamp version);
	
	/**
	 * 新增
	 * @param XbdunSysOrgDo 企业信息
	 */
	@Insert("INSERT INTO xbdun_sys_org(orgCode,orgName,website,orgEmail,valiEmail,addr,logo,type,recordImg,orgMobile,orgLandline,recordNo,limitPers,auditingStatus,remarks,auditingTime,createTime,version,enable,unionid,busiPerson) VALUES(#{a.orgCode},#{a.orgName},#{a.website},#{a.orgEmail},#{a.valiEmail},#{a.addr},#{a.logo},#{a.type},#{a.recordImg},#{a.orgMobile},#{a.orgLandline},#{a.recordNo},#{a.limitPers},#{a.auditingStatus},#{a.remarks},#{a.auditingTime},#{a.createTime},#{a.version},#{a.enable},#{a.unionid},#{a.busiPerson})")
	@SelectKey(statement="SELECT LAST_INSERT_ID() AS ID", before = false, keyProperty = "a.id", resultType = Long.class)
	public Long addXbdunSysOrg(@Param("a")XbdunSysOrgDo xbdunSysOrgDo);
	
	/**
	 * 删除数据根据主键
	 * @param id
	 */
	@Delete("DELETE FROM xbdun_sys_org WHERE id = #{id}")
	public void delXbdunSysOrgById(@Param("id")long id);
	
	/**
	 * 动态更新参数,参数中Map可以必须跟Key为Where的更新条件<br/>
	 * <pre>
	 * 	例如：
	 *  Map<String,Object> setting = new HashMap<String,Object>();
	 *  setting.put("email", "360730440@qq.com");
	 *
	 *  Map<String,Object> where = new HashMap<String,Object>();
	 *  where.put("userId", "1222");
	 * </pre>
	 * Map的key值就是数据库字段<br/>
	 * Map的value就是值
	 */
	@UpdateProvider(type=SysOrgSqlProvider.class,method="updateDynXbdunSysOrgSql")
	public int updateDynXbdunSysOrg(@Param("param")Map<String,Object> param,@Param("_where")Map<String,Object> where);
	
	//------------------------//start分页查询--------------------------------------
	/**
	 * 分页查询 
	 * <pre>
	 * 	例如：
	 *  Map<String,Object> where = new HashMap<String,Object>();
	 *  param.put("orderby", "param.name,param age desc");
	 * </pre>
	 * @param _where where条件，现在支持And条件类型
	 * @param orderBy 排序条件
	 * @param pageNum 当前索引页
	 * @param numPerPage 每页显示条数
	 * @return
	 */
	@SelectProvider(type=SysOrgSqlProvider.class,method="queryPagerSql")
	public List<XbdunSysOrgDo> queryPager(@Param("_where")Map<String,Object> where,@Param("_orderby")String orderBy,@Param("pageIndex")int pageIndex,@Param("pageSize")int pageSize);
	
	/**
	 * 分页查询返回总数量
	 * <pre>
	 * 	例如：
	 *  Map<String,Object> where = new HashMap<String,Object>();
	 *  param.put("orderby", "param.name,param age desc");
	 * </pre>
	 * @param where 参数为where约束参数
	 * @return
	 */
	@SelectProvider(type=SysOrgSqlProvider.class,method="queryPagerCountSql")
	public Long queryPagerCount(@Param("_where")Map<String,Object> where);
	//-------------------------//end--------------------------------------------
	/**
	 * 判断是否存在,不为null则是存在
	 * @param param
	 * @return int
	 */
	@SelectProvider(type=SysOrgSqlProvider.class,method="existSysOrgSql")
	public int existSysOrg(XbdunSysOrgDo xbdunSysOrgDo);

	/**
	 * 查询机构的版本号
	 * @param id
	 * @return String
	 */
	@Select("SELECT version FROM xbdun_sys_org where id=#{id}")
	public Timestamp getSysOrgVersion(@Param("id")long id);
	@Select("SELECT version FROM xbdun_sys_org where orgCode=#{orgCode}")
	public Timestamp getVersionByCode(@Param("orgCode")long orgCode);
	/**
	 * 查询机构的最大机构编码
	 * @param id
	 * @return String
	 */
	@Select("SELECT IFNULL(MAX(orgCode),100000000) + 1 FROM xbdun_sys_org")
	public Long getMaxOrgCode();

	/**
	* @Title: getAgentOrgCode
	* @Description: 获取代理机构编号
	* @return Long    代理机构编号
	 */
	@Select("SELECT orgCode FROM xbdun_sys_org WHERE blAgentOrg=1 LIMIT 1")
	public Long getAgentOrgCode();
	@Select("SELECT COUNT(*) FROM xbdun_sys_org WHERE blAgentOrg=1")
	public int getAgentOrgCodeCnt();

	/**
	 * @Title: getOrgById
	 * @Description: 获取机构
	 * @param id
	 * @return
	 */
	@Select("SELECT * FROM xbdun_sys_org where id=#{id}")
	public XbdunSysOrgDo getOrgById(@Param("id")long id);

	/**根据机构查询到基本信息*/
	//@Select("SELECT a.website,b.pubNumber,b.shopUrl,b.orgResume,b.mainCamp,a.orgName,a.logo imgUrl,a.orgLandline orgTel FROM xbdun_sys_org a LEFT JOIN xbdun_sys_org_fileds b ON a.orgCode = b.orgCode  WHERE a.orgCode = #{orgCode}")
	//public CardOrgInfoDto getCardOrgInfo(String orgCode);
	
	/**机构简介，机构名称，机构介绍*/
	//@Select("SELECT b.orgResume,a.orgName,b.mainCamp FROM xbdun_sys_org a LEFT JOIN xbdun_sys_org_fileds b ON a.orgCode = b.orgCode  WHERE a.orgCode = #{orgCode}")
	//public CardOrgInfoDto getCardOrgInfo02(long orgCode);
}